/**
 * 
 */
package any.note.server.resource;

import org.apache.commons.lang.StringUtils;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;

import any.note.server.NoteService;
import any.restlet.helper.RepresentationHelper;

/**
 * <ul>
 * <li><code>/notes.{type}</code>: GET all notes</li>
 * </ul>
 * 
 * @author keke
 * @reversion $Revision: 23 $
 * @version
 */
public class NotesResource extends Resource {
	private NoteService	         noteService;
	private RepresentationHelper	repHelperManager;

	private String	             type;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.restlet.resource.Resource#init(org.restlet.Context,
	 *      org.restlet.data.Request, org.restlet.data.Response)
	 */
	@Override
	public void init(final Context context, final Request request,
	        final Response response) {
		super.init(context, request, response);
		setModifiable(false);
		type = (String) request.getAttributes().get("type");
		if (StringUtils.isEmpty(type)) {
			type = "html";
		}
		getVariants().add(
		        new Variant(MediaType.valueOf(Utils.FILE_TYPE_MAP
		                .getContentType("Note." + type))));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.restlet.resource.Resource#represent(org.restlet.resource.Variant)
	 */
	@Override
	public Representation represent(final Variant variant)
	        throws ResourceException {
		Representation result = null;
		result = repHelperManager.toRepresentation("notes", noteService
		        .listAll(), variant);
		return result;
	}

	/**
	 * @param noteService
	 *            the noteService to set
	 */
	public void setNoteService(final NoteService noteService) {
		this.noteService = noteService;
	}

	/**
	 * @param repHelperManager
	 *            the repHelperManager to set
	 */
	public void setRepHelperManager(final RepresentationHelper repHelperManager) {
		this.repHelperManager = repHelperManager;
	}

}
